﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Runtime.Remoting.Channels;
using AutoStar.Models;
using System.Configuration;
using System.Windows.Media;
using Color = System.Drawing.Color;
using System.Globalization;
using System.Windows.Input;
using Newtonsoft.Json;

namespace AutoStar.Common
{
    [Serializable]
    public class PrintProject
    {
        public string id;
        public string name;
        public string description;

        public Model3D model;

        public List<PrintLayer> printLayers = new List<PrintLayer>();//打印层
        public List<PrintChannel> printChannels = new List<PrintChannel>();//打印通道

        public DateTime createTime;
        public DateTime updateTime;
        public List<int> ChannelNumList { get; set; }
        public List<string> ChannelTypes { get; set; }
        public PrintProject()
        {
            // 当前日期转换成时间戳
            long time1 = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
            id = time1.ToString();
            name = "（新建项目）";
            model = new Model3D();
            //初始化通道
            //printChannels.Add(new PrintChannel(1, Color.Salmon, new System.Windows.Point(0, 0), 25.35, 25, 600, 3, 10, 100));
            //printChannels.Add(new PrintChannel(2, Color.ForestGreen, new System.Windows.Point(0, 0), 25.35, 25, 600, 3, 10, 100));
            //printChannels.Add(new PrintChannel(3, Color.MediumBlue, new System.Windows.Point(0, 0), 25.35, 25, 600, 3, 10, 100));
            //printChannels.Add(new PrintChannel(4, Color.Crimson, new System.Windows.Point(0, 0), 25.35, 25, 600, 3, 10, 100));
            //printChannels.Add(new PrintChannel(5, Color.Yellow, new System.Windows.Point(0, 0), 25.35, 25, 600, 3, 10, 100));
            //printChannels.Add(new PrintChannel(6, Color.Yellow, new System.Windows.Point(0, 0), 0, 0, 0, 0, 0, 0));
            try
            {
                //if (ConfigurationManager.AppSettings.AllKeys.Contains("Channel5Param7"))
                //{
                //    List<ChannelList> channels = new List<ChannelList>();
                //    for (int i = 1; i <= 7; i++)
                //    {
                //        ChannelList channel = new ChannelList();
                //        if (i <= 7)
                //            channel.Name = "通道" + i;
                //        //else
                //        //    channel.Name = "调试通道";
                //        channel.Parameters = new PrinChannelModel();
                //        for (int j = 1; j <= 7; j++)
                //        {
                //            string key = "Channel" + i + "Param" + j;
                //            string v = Global.encryption.Decrypt(ConfigurationManager.AppSettings[key]);
                //            double value = Math.Round(Convert.ToDouble(v), 2);
                //            if (j == 1)
                //                channel.Parameters.text21 = value;
                //            else if (j == 2)
                //                channel.Parameters.text22 = value;
                //            else if (j == 3)
                //                channel.Parameters.text23 = value;
                //            else if (j == 4)
                //                channel.Parameters.text24 = value;
                //            else if (j == 5)
                //                channel.Parameters.text25 = value;
                //            else if (j == 6)
                //                channel.Parameters.text26 = value;
                //            else if (j == 7)
                //                channel.Parameters.text27 = value;
                //            else if (j == 8)
                //                channel.Parameters.text28 = value;
                //        }
                //        channels.Add(channel);
                //    }
                if (ConfigurationManager.AppSettings.AllKeys.Contains("Channel5"))
                {
                    List<ChannelList> channels = new List<ChannelList>();
                    for (int i = 1; i <= 7; i++)
                    {
                        ChannelList channel = new ChannelList();
                        if (i <= 7)
                            channel.Name = "通道" + i;
                        channel.Parameters = new PrinChannelModel();
                        string key = "Channel" + i;
                        string encryptedValue = ConfigurationManager.AppSettings[key];
                        string decryptedValue = Global.encryption.Decrypt(encryptedValue);
                        PrinChannelModel parameters = JsonConvert.DeserializeObject<PrinChannelModel>(decryptedValue);
                        channel.Parameters = parameters;
                        channels.Add(channel);
                    }
                    List<Color> colors = new List<Color>() { Color.Salmon, Color.ForestGreen, Color.MediumBlue, Color.Crimson, Color.Yellow, Color.Red, Color.Black };
                    if (ConfigurationManager.AppSettings.AllKeys.Contains("ChannelTypes"))
                    {
                        string channelTypes = ConfigurationManager.AppSettings["ChannelTypes"];
                        ChannelTypes = channelTypes.Split(',').ToList();
                    }
                    else
                    {
                        ChannelTypes = new List<string>
                                {
                                    "挤出模式","挤出模式","降温通道","电磁阀点喷模式","电磁阀点喷模式","电磁阀点喷模式" ,"升温通道" /*,"调试通道"*/
                                };
                    }
                    if (ConfigurationManager.AppSettings.AllKeys.Contains("ChannelNumList"))
                    {
                        string channelNumList = ConfigurationManager.AppSettings["ChannelNumList"];
                        ChannelNumList = channelNumList.Split(',').ToList().Select(int.Parse).ToList();
                    }
                    else
                    {
                        ChannelNumList = new List<int>
                                {
                                    1,2,3,4,5,6,7/*,1*/
                                };
                    }
                    for (int i = 0; i < channels.Count; i++)
                    {
                        try
                        {
                            PrintChannel printChannel = new PrintChannel(ChannelNumList[i], colors[i],
                                                new Point3D((float)channels[i].Parameters.text27, (float)channels[i].Parameters.text28, 0),
                                                (float)channels[i].Parameters.text21, (float)channels[i].Parameters.text22, (float)channels[i].Parameters.text23,
                                                (float)channels[i].Parameters.text24, (float)channels[i].Parameters.text25, (float)channels[i].Parameters.text26, ChannelTypes[i]);
                            printChannels.Add(printChannel);
                            //printChannels.Add(new PrintChannel(ChannelNumList[i], colors[i],
                            //                    new Point3D((float)channels[i].Parameters.text27, (float)channels[i].Parameters.text28, 0),
                            //                    (float)channels[i].Parameters.text21, (float)channels[i].Parameters.text22, (float)channels[i].Parameters.text23,
                            //                    (float)channels[i].Parameters.text24, (float)channels[i].Parameters.text25, (float)channels[i].Parameters.text26));
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                    //特殊处理 通道2是 通道4的y-12变化
                    //printChannels[1].position.X = (float)channels[3].Parameters.text27;
                    //printChannels[1].position.Y = (float)channels[3].Parameters.text28 - 12;
                }
                else
                {
                    printChannels.Add(new PrintChannel(1, Color.Salmon, new Point3D(90, 9, 0), 4, 25, 600, 3, 10, 100, "挤出模式"));
                    printChannels.Add(new PrintChannel(2, Color.ForestGreen, new Point3D((float)66.5, 9, 0), 5, 25, 600, 3, 10, 100, "挤出模式"));
                    printChannels.Add(new PrintChannel(3, Color.MediumBlue, new Point3D(39, 10, 0), 13, 25, 600, 3, 10, 100, "降温通道"));
                    printChannels.Add(new PrintChannel(4, Color.Crimson, new Point3D(0, 0, 0), 4, 25, 600, 3, 10, 100, "电磁阀点喷模式"));
                    printChannels.Add(new PrintChannel(5, Color.Yellow, new Point3D(0, 10, 0), 5, 25, 600, 3, 10, 100, "电磁阀点喷模式"));
                    printChannels.Add(new PrintChannel(6, Color.Red, new Point3D(0, 20, 0), 6, 25, 600, 3, 10, 100, "电磁阀点喷模式"));
                    printChannels.Add(new PrintChannel(7, Color.YellowGreen, new Point3D((float)10.7, 43, 0), 7, 25, 600, 3, 10, 100, "升温通道"));
                    //printChannels.Add(new PrintChannel(1, Color.Black, new Point3D(0, 0, 0), 0, 0, 600, 3, 10, 100, "调试通道"));
                }
                createTime = DateTime.Now;
                updateTime = DateTime.Now;
                description = "";
            }
            catch (Exception)
            {
            }
        }
    }
}
